/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.notify;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.cache.NotifyReceiveRuleCache;
import com.sinodata.bsm.center.service.notify.NotifyReceiveRuleService;
import com.sinodata.bsm.center.user.UserUtil;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.constants.NotifyConstants;
import com.sinodata.bsm.common.utils.CrontabUtil;
import com.sinodata.bsm.common.vo.NotifyReceiveRule;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-9 下午5:05:58          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class NotifyReceiveRuleAction extends BaseAction<NotifyReceiveRule> {

    /**
     * 
     */
    private static final long serialVersionUID = -1946313323287134459L;
    @Autowired
    private NotifyReceiveRuleCache notifyReceiveRuleCache;
    @Autowired
    private NotifyReceiveRuleService notifyReceiveRuleService;
    private Long id;
    private NotifyReceiveRule entity;

    @Override
    public NotifyReceiveRule getModel() {
        return entity;
    }

    public String common() {
        return "common";
    }

    @Override
    public String list() throws Exception {
        List<NotifyReceiveRule> list = new ArrayList<NotifyReceiveRule>();
        if (!request.getParameter("notifyRuleId").equals("")) {
            list = notifyReceiveRuleCache.getByNotifyRule(Long.valueOf(request.getParameter("notifyRuleId")));
        }
        JSONObject json = new JSONObject();
        json.element("iTotalRecords", list.size());
        json.element("iTotalDisplayRecords", list.size());
        JSONArray array = new JSONArray();
        for (NotifyReceiveRule notifyReceiveRule : list) {
            JSONObject obj = new JSONObject();
            obj.element("op", "<div style='width: 70px'>&nbsp;&nbsp;&nbsp;<a href='javascript:update(" + notifyReceiveRule.getId() + ");'><img src='../../style/default/bsm/images/edit.gif' title='修改'></a>&nbsp<a href='javascript:del(" + notifyReceiveRule.getId() + ");'><img src='../../style/default/bsm/images/delete.gif' title='删除'></a><div>");
            obj.element("id", notifyReceiveRule.getId());
            obj.element("user", UserUtil.getInstance().getUserById(notifyReceiveRule.getUser()) == null ? "此用户已被删除" : UserUtil.getInstance().getUserById(notifyReceiveRule.getUser()).getFullName());
            obj.element("notifyMode", NotifyConstants.getModeDesc(notifyReceiveRule.getNotifyMode()));
            obj.element("times", notifyReceiveRule.getTimes());
            obj.element("interval", notifyReceiveRule.getInterval() == 0 ? "0" : CrontabUtil.parseSimple(notifyReceiveRule.getInterval()));
            array.add(obj);
        }
        json.element("aaData", array);
        renderText(json.toString());
        return null;
    }

    /**
     * 转向新增通知接收者页面
     * @return
     * @throws Exception
     */
    public String add() {
        Struts2Utils.getRequest().setAttribute("notifyRuleId", request.getParameter("notifyRuleId"));
        return "add";
    }

    @Override
    public String input() throws Exception {
        @SuppressWarnings("deprecation")
        NotifyReceiveRule notifyReceiveRule = notifyReceiveRuleCache.get(Long.valueOf(request.getParameter("id")));
        Struts2Utils.getRequest().setAttribute("notifyReceiveRule", notifyReceiveRule);
        Struts2Utils.getRequest().setAttribute("userName", UserUtil.getInstance().getUserById(notifyReceiveRule.getUser()) == null ? "此用户已删除" : UserUtil.getInstance().getUserById(notifyReceiveRule.getUser()).getFullName());
        return "update";
    }

    public String checkDeleted() {
        JSONObject ob = new JSONObject();
        if (notifyReceiveRuleCache.get(Long.valueOf(request.getParameter("id"))) == null) {
            ob.element("flag", false);
        } else {
            ob.element("flag", true);
        }
        Struts2Utils.renderJson(ob.toString());
        return null;
    }

    @Override
    public String save() throws Exception {
        if (entity.getId() != null) {//修改
            if (notifyReceiveRuleCache.get(entity.getId()) != null) {
                //校验用户和方式是否已存在
                String oldNotifyMode = request.getParameter("oldNotifyMode");
                if (!Integer.valueOf(oldNotifyMode).equals(entity.getNotifyMode())) {
                    List<NotifyReceiveRule> list = new ArrayList<NotifyReceiveRule>();
                    list.add(entity);
                    if (!notifyReceiveRuleService.validateNotifyReceiveRule(list, notifyReceiveRuleCache.getByNotifyRule(Long.valueOf(request.getParameter("notifyRuleId"))))) {
                        JSONObject ob = new JSONObject();
                        ob.element("flag", false);
                        Struts2Utils.renderJson(ob.toString());
                        return null;
                    }
                }
                notifyReceiveRuleService.update(entity);
                notifyReceiveRuleCache.put(entity);
            } else {
                JSONObject ob = new JSONObject();
                ob.element("del", true);
                Struts2Utils.renderJson(ob.toString());
                return null;
            }
        } else {//新增
            List<NotifyReceiveRule> list = new ArrayList<NotifyReceiveRule>();
            for (String user : entity.getUser().split(",")) {
                NotifyReceiveRule notifyReceiveRule = new NotifyReceiveRule();
                notifyReceiveRule.setUser(user);
                notifyReceiveRule.setInterval(entity.getInterval());
                notifyReceiveRule.setTimes(entity.getTimes());
                notifyReceiveRule.setNotifyMode(entity.getNotifyMode());
                notifyReceiveRule.setNotifyRuleId(Long.valueOf(request.getParameter("notifyRuleId")));
                list.add(notifyReceiveRule);
            }
            //校验用户和方式是否已存在
            if (!notifyReceiveRuleService.validateNotifyReceiveRule(list, notifyReceiveRuleCache.getByNotifyRule(Long.valueOf(request.getParameter("notifyRuleId"))))) {
                JSONObject ob = new JSONObject();
                ob.element("flag", false);
                Struts2Utils.renderJson(ob.toString());
                return null;
            }
            notifyReceiveRuleService.add(list);
            for (NotifyReceiveRule notifyReceiveRule : list) {
                notifyReceiveRuleCache.put(notifyReceiveRule);
            }
        }
        JSONObject ob = new JSONObject();
        ob.element("flag", true);
        Struts2Utils.renderJson(ob.toString());
        return null;
    }

    @Override
    public String delete() throws Exception {
        notifyReceiveRuleService.delete(id);
        notifyReceiveRuleCache.remove(id);
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = notifyReceiveRuleCache.get(id);
            if (entity == null) {
                entity = new NotifyReceiveRule();
            }
        } else {
            entity = new NotifyReceiveRule();
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}
